import { TimeoutException } from "./TimeoutException";
import { Context } from "./Context";
import { SerializeStream } from "../../Framework/Serialize/SerializeStream";
import { ERMIDispatchStatus } from "./ERMIDispatchStatus";
import { Exception } from "../../Framework/Util/Exception";
import { SRMIReturn } from "./SRMIReturn";
import { Outgoing } from "./Outgoing";
import { Session } from "./Session";
import { RMIObject } from "./RMIObject";
import { getTimer, Timer } from "../../flash/utils";
type int = number;
//class RMIObjectBind

export class RMIObjectBind {
  constructor() {
    this._outgoingStatus = RMIObjectBind.EOutGoingWaitToSend;
    this._createTimer = getTimer();
  }

  public initTimer(arg1: int): void {
    if (arg1 >= 0) {
      this._timer = new Timer(/* flash.utils.Timer */ arg1, 1);
      this._timer.addEventListener("timer_complete", this.onComplete.bind(this));
      this._timer.start();
    }
    return;
  }

  public resertTimer(): void {
    if (this._timer) {
      this._timer.reset();
      this._timer = undefined;
    }
  }

  private onComplete(arg1: Object /* flash.events.TimerEvent */): void {
    this._session.removeBackObject(this.messageId);
    if (null != this._timer) {
      this._timer.reset();
    }
    this._outgoingStatus = RMIObjectBind.EOutGoingTimeOut;
    if (this._rmiObject != null) {
      var exception = new TimeoutException();
      if (this._session.communicator.prepareCommandHandler) {
        this._rmiObject.usedTimer = getTimer() - this._createTimer;
        this._session.communicator.prepareCommandHandler.prepareBackException(
          this._rmiObject,
          exception
        );
      }
      this._rmiObject.cdeException(exception);
    }
    return;
  }

  public __back(context: Context, stream: SerializeStream): void {
    this._outgoingStatus = RMIObjectBind.EOutGoingComplate;
    if (context._dispachStatus.equals(ERMIDispatchStatus.DispatchOK)) {
      if (this._session.communicator.prepareCommandHandler) {
        this._rmiObject.usedTimer = getTimer() - this._createTimer;
        this._session.communicator.prepareCommandHandler.prepareBackCommand(
          this._rmiObject
        );
      }
      this.rmiObject.__response(stream);
    } else if (
      context._dispachStatus.equals(ERMIDispatchStatus.DispatchException)
    ) {
      var exception = new Exception();
      stream.readException(exception);
      if (this._session.communicator.prepareCommandHandler) {
        this._rmiObject.usedTimer = getTimer() - this._createTimer;
        this._session.communicator.prepareCommandHandler.prepareBackException(
          this._rmiObject,
          exception
        );
      }
      this._rmiObject.cdeException(exception);
    }
  }

  public __response(arg1: ERMIDispatchStatus, arg2: SerializeStream): void {
    if (this._messageId == 0) {
      return;
    }
    var srmiReturn = new SRMIReturn();
    srmiReturn.messageId = this._messageId;
    srmiReturn.dispatchStatus = arg1;
    var context = new Context();
    context._session = this.session;
    context._connection = this.session.connection;
    Outgoing.dispatch(context, srmiReturn, arg2);
    return;
  }

  public __exception(arg1: Exception): void {
    if (this._messageId == 0) {
      return;
    }
    var srmiReturn = new SRMIReturn();
    srmiReturn.messageId = this._messageId;
    srmiReturn.dispatchStatus = ERMIDispatchStatus.DispatchException;
    var context = new Context();
    context._session = this.session;
    context._connection = this.session.connection;
    var stream = new SerializeStream();
    stream.writeException(arg1);
    Outgoing.dispatch(context, srmiReturn, stream);
    return;
  }

  public set session(arg1: Session) {
    this._session = arg1;
    return;
  }

  public get session(): Session {
    return this._session;
  }

  public set rmiObject(arg1: RMIObject) {
    this._rmiObject = arg1;
    return;
  }

  public get rmiObject(): RMIObject {
    return this._rmiObject;
  }

  public set buffer(arg1: SerializeStream) {
    this._buffer = arg1;
    return;
  }

  public get buffer(): SerializeStream {
    return this._buffer;
  }

  public set outgoingStatus(arg1: int) {
    this._outgoingStatus = arg1;
    return;
  }

  public get outgoingStatus(): int {
    return this._outgoingStatus;
  }

  public set messageId(arg1: int) {
    this._messageId = arg1;
    return;
  }

  public get messageId(): int {
    return this._messageId;
  }

  public static /* const */ EOutGoingWaitToSend: int = 0;

  public static /* const */ EOutGoingTimeOut: int = 1;

  public static /* const */ EOutGoingComplate: int = 2;

  private /* var */ _session!: Session;

  private /* var */ _rmiObject!: RMIObject;

  private /* var */ _buffer!: SerializeStream;

  private /* var */ _outgoingStatus!: int;

  private /* var */ _messageId!: int;

  private /* var */ _timer?: Timer /* flash.utils.Timer */;

  private /* var */ _createTimer: number;
}
